package com.example.expandprogresslist;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class MainActivity extends AppCompatActivity {
    private ExpandableListView listView;
    private ExpandableListViewAdapter adapter;
    private List<ExpandDataEntity> dataEntityList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.list);
        dataEntityList.addAll(getData());
        adapter = new ExpandableListViewAdapter(this, dataEntityList);
        listView.setAdapter(adapter);

        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < adapter.getGroupCount(); i++) {
                    for (int x = 0; x < adapter.getChildrenCount(i); x++) {
                        ExpandableListViewAdapter.ChildViewHolder viewHolder = adapter.getChildViewHolder(i, x);
                        if (viewHolder != null) {
                            Random random = new Random();
                            int i2 = random.nextInt(100);
                            viewHolder.progressBar.setProgress(i2);
                        }
                    }
                }
                handler.postDelayed(this,200);
            }
        });
    }

    private List<ExpandDataEntity> getData() {
        List<ExpandDataEntity> dataEntityList = new ArrayList<>();
        int groupCount = 5;
        int itemCount = 20;
        for (int i = 0; i < groupCount; i++) {
            List<ExpandItemEntity> itemEntityList = new ArrayList<>();
            for (int x = 0; x < itemCount; x++) {
                ExpandItemEntity itemEntity = new ExpandItemEntity("子任务: " + x, 0);
                itemEntityList.add(itemEntity);
            }
            ExpandDataEntity dataEntity = new ExpandDataEntity();
            dataEntity.setData(itemEntityList);
            dataEntity.setTitle("下载任务: " + i);
            dataEntityList.add(dataEntity);
        }
        return dataEntityList;
    }
}
